/*
 * VHClubHistoryTab.java
 * Created on 22 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.tabs;

import java.util.List;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.rpc.VHRpcCallback;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.widget.VHClubContestHistoryTypeSelector;
import com.hockeo.vhweb.client.ui.widget.VHClubHistoryGrid;
import com.hockeo.vhweb.client.ui.widget.VHClubSeasonSummaryGrid;
import com.hockeo.vhweb.client.ui.widget.VHClubHistoryGrid.HistoryDisplayType;
import com.hockeo.vhweb.client.utils.VHJsData;
import com.hockeo.vhweb.client.utils.enums.VHJsClubContestHistoryType;

/**
 * Provides a select box to select the type of history to view and shows the records for
 * the chosen history type.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHClubHistoryTab extends VHTabItem
{
  private VHJsData                         d_club;
  private VHJsClubContestHistoryType       d_typeCurrent;
  private VHClubContestHistoryTypeSelector d_contestTypeSelector;
  private VHClubSeasonSummaryGrid          d_seasonSummaryGrid;
  private VHClubHistoryGrid                d_historyGrid;
  private BorderLayoutData                 d_layoutDataCenter;
  private Widget                           d_widgetCurrent;

  public VHClubHistoryTab( VHController controller, VHJsData club )
  {
    super( controller, VHMain.getConstants().seasonHistory() );

    d_club = club;

    setIcon( VHMain.ICON_HISTORY );
  }

  public void setClub( VHJsData club )
  {
    if ( d_club != null && d_club.getId().equals( club.getId() ) )
      return;

    d_club = club;

    // Reset currently displayed type so that the data is reloaded
    //
    d_typeCurrent = null;
  }

  @Override
  public void buildContent()
  {
    BorderLayoutData data;

    if ( isContentReady() )
    {
      // if there is currently no type selected, the club had been reset, so let's select
      // the season summary in the contest-type dropdown
      //
      if ( d_typeCurrent == null )
        d_contestTypeSelector.selectDefault();

      return;
    }

    setLayoutOnChange( true );

    // Northern settings panel (contest type selector)
    //
    ContentPanel panelNorth = new ContentPanel( new RowLayout( Style.Orientation.VERTICAL ) );
    panelNorth.setHeaderVisible( false );

    d_contestTypeSelector = new VHClubContestHistoryTypeSelector();
    panelNorth.add( d_contestTypeSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 4, 4, 4, 4 ) ) );

    data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 32f );
    add( panelNorth, data );

    // event listeners
    //
    d_contestTypeSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        VHJsData contestType = se.getSelectedItem();
        onContestTypeSelected( contestType );
      }
    } );

    // Reselect the default value so that initial data loading is triggered
    //
    d_contestTypeSelector.selectDefault();

    super.buildContent();
  }

  /**
   * Sets the main panel area based on the selected contest type.
   *
   * This method should be called when the contest type selector has received its data and
   * then every time the user chooses a different contest type.
   */
  public void onContestTypeSelected( VHJsData contestTypeSelected )
  {
    if ( d_layoutDataCenter == null )
    {
      d_layoutDataCenter = new BorderLayoutData( LayoutRegion.CENTER );
      d_layoutDataCenter.setMargins( new Margins( 0, 4, 4, 4 ) );
    }

    VHJsClubContestHistoryType type = VHJsClubContestHistoryType.valueOf( contestTypeSelected.getId() );

    // There is no need to do anything, if the type is the same as before
    //
    if ( d_typeCurrent != null && d_typeCurrent == type )
      return;

    // Remove current widget (if there is one)
    //
    if ( d_widgetCurrent != null )
      d_widgetCurrent.removeFromParent();

    // Create/reset new widget
    //
    switch ( type )
    {
      case SEASON_SUMMARY:
      {
        if ( d_seasonSummaryGrid == null )
          d_seasonSummaryGrid = new VHClubSeasonSummaryGrid();

        VHController.getBaseDataService().getClubSeasonSummaries( VHController.getLocale(), d_club.getId(),
            new VHRpcCallback<List<VHJsData>>( d_seasonSummaryGrid, null ) );

        d_widgetCurrent = d_seasonSummaryGrid;
        break;
      }

      case INTERNATIONAL_CUP:
      case NATIONAL_CUP:
      case NATIONAL_LEAGUE:
      {
        if ( d_historyGrid == null )
          d_historyGrid = new VHClubHistoryGrid( getController(), HistoryDisplayType.SINGLE_CLUB );

        VHController.getBaseDataService().getClubContestHistoryRecords( VHController.getLocale(), d_club.getId(), type,
            new VHRpcCallback<List<VHJsData>>( d_historyGrid, type.name() ) );

        d_widgetCurrent = d_historyGrid;
        break;
      }

      default:
        Window.alert( "Unsupported contest type " + type.name() );
    }

    // Add new widget to the parent panel
    //
    add( d_widgetCurrent, d_layoutDataCenter );
  }
}
